package fuck;

import java.util.Arrays;
import java.util.Objects;

public class Animal {
    private Object o;
    private static int id = 1;

    static {
        System.out.println("static block in Animal");
    }

    Animal() {
        id++;
    }

    public final void stable() {
        System.out.println("final method");
    }

    private String name;

    @Override
    public int hashCode() {
        return Objects.hash(id, o);
    }

    /**
     * 标准的覆盖equals方法
     *
     * @param otherObject 比较的对象
     * @return 相同, true, 否则false
     */
    @Override
    public boolean equals(Object otherObject) {
        if (this == otherObject) {
            return true;
        }
        if (otherObject == null) {
            return false;
        }
        // 非继承的判断, 或者子类中需要用getClass()
        if (!(otherObject instanceof Animal)) {
            return false;
        }
        Animal other = (Animal) otherObject;
        // 判断类域, 基本类型使用==, 引用类型使用equals
        // Objects.equals可以检测null, 方便
        return Objects.equals(other.name, this.name);
    }

    @Override
    public String toString() {
        return this.getClass().getName();
    }

    public static void show() {
        System.out.println("Animal static show()");
    }

    public static void main(String[] args) {
        Animal a1 = new Animal();
        Animal a2 = a1;
        Animal a3 = new Animal();
//        System.out.println(a1.hashCode());
//        System.out.println(a2.hashCode());
//        测试Arrays.hashCode
//        System.out.println(Arrays.hashCode(new int[]{1, 2, 3, 4}));
//        System.out.println(Arrays.hashCode(new int[]{1, 2, 3, 4}));
//        System.out.println(Double.hashCode(3));
//        System.out.println(new Animal());
        System.out.println(a1 == a3);
        System.out.println(a1.equals(a3));
    }
}
